package com.jwater.core.net;

import java.io.Serializable;
import java.util.Arrays;

import jwater.org.apache.mina.core.buffer.IoBuffer;
/**
 * @description:
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class ResultPacket implements Serializable {
	private static final long serialVersionUID = -986578938859130435L;

	private int resultLen;
	private byte[] result;

	public byte[] serialize() throws Exception {
		IoBuffer ioBuffer = IoBuffer.allocate(4 + resultLen);
		ioBuffer.putInt(resultLen);
		if (resultLen > 0) {
			ioBuffer.put(result);
		}
		return ioBuffer.array();
	}

	public ResultPacket deserialize(byte[] data) throws Exception {
		IoBuffer ioBuffer = IoBuffer.allocate(data.length);
		ioBuffer.put(data);
		ioBuffer.flip();
		resultLen = ioBuffer.getInt();
		if (resultLen > 0) {
			byte[] temp = new byte[resultLen];
			ioBuffer.get(temp);
			result = temp;
		}
		return this;
	}

	public int getResultLen() {
		return resultLen;
	}

	public void setResultLen(int resultLen) {
		this.resultLen = resultLen;
	}

	public byte[] getResult() {
		return result;
	}

	public void setResult(byte[] result) {
		this.result = result;
	}

	@Override
	public String toString() {
		return "ResultPacket [resultLen=" + resultLen + ", result=" + Arrays.toString(result) + "]";
	}
}
